package com.bailugansan.efficiencyboard.service.impl;

import com.bailugansan.efficiencyboard.dao.PoolDao;
import com.bailugansan.efficiencyboard.entity.pool.AddPoolEntity;
import com.bailugansan.efficiencyboard.entity.pool.RemovePoolEntity;
import com.bailugansan.efficiencyboard.entity.pool.UpdatePoolEntity;
import com.bailugansan.efficiencyboard.entity.task.AddTaskEntity;
import com.bailugansan.efficiencyboard.pojo.Pool;
import com.bailugansan.efficiencyboard.service.PoolService;
import com.bailugansan.efficiencyboard.service.TaskService;
import com.bailugansan.efficiencyboard.vo.BoardVo;
import com.bailugansan.efficiencyboard.vo.PoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author BailuGansan
 */
@Service
public class PoolServiceImpl implements PoolService {
    @Autowired
    private PoolDao poolDao;
    @Autowired
    private TaskService taskService;

    @Override
    public boolean addPool(AddPoolEntity entity, String uid) {

        String drawerTitle = entity.getDrawerTitle();
        String drawerColor = entity.getDrawerColor();

        boolean result = poolDao.addPool(drawerTitle, drawerColor, uid);

        int poolId = poolDao.getPoolId(drawerTitle, uid);

        AddTaskEntity addTaskEntity = new AddTaskEntity();
        addTaskEntity.setPdd(poolId);
        addTaskEntity.setDrawerPriority("锚点");

        taskService.addTask(addTaskEntity);

        return result;
    }

    @Override
    public boolean updatePool(UpdatePoolEntity entity, String uid) {

        int poolId = entity.getPoolId();
        String drawerTitle = entity.getDrawerTitle();
        String drawerColor = entity.getDrawerColor();

        boolean result = poolDao.updatePool(poolId, drawerTitle, drawerColor, uid);

        return result;
    }

    @Override
    public boolean removePool(RemovePoolEntity entity, String uid) {

        int poolId = entity.getPoolId();

        boolean result = poolDao.removerPool(poolId, uid);

        return result;
    }

    @Override
    public List<PoolVo> getPoolVos(String uid) {
        List<Pool> pools =  poolDao.getPools(uid);
        List<PoolVo> collect = pools.stream().map(pool -> {
            PoolVo poolVo = new PoolVo();
            // 这里也是硬转
            poolVo.setPoolId(pool.getPool_id());
            poolVo.setPoolName(pool.getPool_name());
            poolVo.setColor(pool.getColor());
            List<BoardVo> boardVos = taskService.getTaskVos(poolVo.getPoolId());
            poolVo.setBoards(boardVos);
            return poolVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean init(String userName) {

        boolean result = poolDao.initPool(userName);

        return result;
    }
}
